package com.ruoyi.web.controller.laiqianba.merchant;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.db.domain.MerchantBaseInfo;
import com.ruoyi.db.model.MerchantBaseInfoParam;
import com.ruoyi.web.service.IMerchantApplyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.MerchantApplyInfo;
import com.ruoyi.merchant.service.IMerchantApplyInfoService;
import com.ruoyi.web.wrapper.MerchantApplyInfoWrapper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商户申请信息Controller
 * 
 * @author ruoyi
 * @date 2024-06-24
 */
@RestController
@RequestMapping("/merchant/applyInfo")
public class MerchantApplyInfoController extends BaseController
{
    @Autowired
    private IMerchantApplyInfoService merchantApplyInfoService;
    @Autowired
    private IMerchantApplyService merchantApplyService;

    /**
     * 查询商户申请信息列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:applyInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(MerchantApplyInfo merchantApplyInfo)
    {
        Long userId = getUserId();
        if (!SecurityUtils.loginUserIsAdmin()){
            merchantApplyInfo.setOwnerId(userId);
        }
        startPage();
        List<Map<String, Object>> list = merchantApplyInfoService.selectMerchantApplyInfoMapList(merchantApplyInfo);
        list = new MerchantApplyInfoWrapper(list).wrap();
        return getDataTable(list);
    }

    /**
     * 导出商户申请信息列表
     */
    @PreAuthorize("@ss.hasPermi('merchant:applyInfo:export')")
    @Log(title = "商户申请信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MerchantApplyInfo merchantApplyInfo)
    {
        List<MerchantApplyInfo> list = merchantApplyInfoService.selectMerchantApplyInfoList(merchantApplyInfo);
        ExcelUtil<MerchantApplyInfo> util = new ExcelUtil<MerchantApplyInfo>(MerchantApplyInfo.class);
        util.exportExcel(response, list, "商户申请信息数据");
    }

    /**
     * 获取商户申请信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:applyInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        MerchantApplyInfo merchantApplyInfo = merchantApplyInfoService.selectMerchantApplyInfoById(id);

        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(merchantApplyInfo));

        try {
            if (StrUtil.isNotEmpty(merchantApplyInfo.getBusinessLicenseArray())){
                jsonObject.put("bsArray", JSON.parseArray(merchantApplyInfo.getBusinessLicenseArray()));
            }
            if (StrUtil.isNotEmpty(merchantApplyInfo.getOtherInfo())){
                jsonObject.put("otherInfoArray", JSON.parseArray(merchantApplyInfo.getOtherInfo()));
            }
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return success(jsonObject);
    }

    /**
     * 新增商户申请信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:applyInfo:add')")
    @Log(title = "商户申请信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MerchantApplyInfo merchantApplyInfo)
    {
        return toAjax(merchantApplyInfoService.insertMerchantApplyInfo(merchantApplyInfo));
    }

    /**
     * 修改商户申请信息
     */
//    @PreAuthorize("@ss.hasPermi('merchant:applyInfo:edit')")
//    @Log(title = "商户申请信息", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody MerchantApplyInfo merchantApplyInfo,
//                           @RequestBody MerchantBaseInfoParam merchantBaseInfoParam)
//    {
//        return toAjax(merchantApplyService.passMerchantApply(merchantApplyInfo, merchantBaseInfoParam));
//    }

    /**
     * 修改商户申请信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:applyInfo:edit')")
    @Log(title = "商户申请信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MerchantApplyInfo merchantApplyInfo)
    {
        return toAjax(merchantApplyService.passMerchantApply(merchantApplyInfo));
    }

    /**
     * 删除商户申请信息
     */
    @PreAuthorize("@ss.hasPermi('merchant:applyInfo:remove')")
    @Log(title = "商户申请信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(merchantApplyInfoService.deleteMerchantApplyInfoByIds(ids));
    }
}
